package com.aita.diamond.service

import com.gd.magic.Service
import com.gd.magic.util.StringUtil

import com.gd.domain.party.Organization
 
import org.hibernate.Query

import java.text.*
import com.gd.domain.party.Operator

import com.aita.diamond.Order
import com.aita.diamond.VIPCustomer
import com.aita.diamond.Diamond
import com.aita.diamond.FactoryBill
 /**
  * 本类提供发卡充值点柜台业务相关的功能
  * @version V0.2
  * @copyright(c) Copyright G&D Corporation 2007.
  * @author zhanghuanxun
  */
class OrderService implements Service {
			
	public static final String FORMAT_YEARMONTH = "yyyyMM"
	
	def loadOrder(long id){
		def now = new Date();
		Order order = Order.load(id)
		assert order : X("未找到单据"+ id)
		
		def salesoper = order.salesoper
		assert salesoper : X("销售人员未找到"+ id)
		
		Diamond diamond = Diamond.loadByOrder(order)
		assert diamond : X("钻石信息为空")
//		assert diamond.factoryBill : X("加工单信息为空")
		 
		def result = [order:order,customer:order.customer,org:diamond.org,diamond:diamond,factoryBill:diamond.factoryBill]
		return result;
	}
	
	//添加订购单 销售单
	def saveOrder(Order order, VIPCustomer customer, Diamond diamond, FactoryBill factorybill){
		assert customer: X("客户信息为空")
		assert customer.name: X("客户姓名为空")
		assert diamond: X("钻石信息为空")
		assert factorybill: X("加工单信息为空")
		assert order: X("订单为空") 
		assert order.orderStatus != 1  : X("非新建状态不能编辑")

		def e = VIPCustomer.loadByPhone(customer.phone)
		
		//客户处理
		if(e == null)
		{
			customer.salesStatus = 1;//已购买
			customer.persistOrMerge()//TODO 判断是否存在
		}
		
		if(order.id == null) // new
		{
			order.customer = customer
			order.orderStatus = 1 //new
			
			factorybill.factoryBillStatus =1 //new
			
			if(diamond.id != null)
			{
				order.orderType = 2  //销售单
				diamond.diamondStatus = 2  //售出
			}
			else
			{
				diamond.org = loginService.currentOrg()
				diamond.diamondStatus = 3  //定制中  
				diamond.type = 2 // 订购采购
				order.orderType = 1  //订购单

			}	
			
		}else //edit
		{
			assert order.orderStatus == 1 || order.orderStatus == 2 : X("非新建和提交状态不能编辑")
			assert factorybill.factoryBillStatus == 1  : X("加工单已发不能编辑")
			//TODO 判断工单状态 已发 不允许编辑
		}

		logService.log(order.id == null?101001:101002,(order.id == null ? "新增" : "修改"+order.id)+"订单")
		
		order.persistOrMerge()
		
		diamond.factoryBill = factorybill
		diamond.order = order
		
		factorybill.persistOrMerge()//保存加工单
		diamond.persistOrMerge()//保存钻石信息
		
		
		
		
	}
	
	
	def commitOrder(long id){
		logger.debug( "  id...............") 
		def now = new Date();
		Order order = Order.load(id)
		assert order : X("未找到"+ id)
		assert order.orderType == 1 : X("有现货不需要提交")
		assert order.orderStatus == 1  : X("非新建状态不能提交")
		order.orderStatus = 2 //提交
		logService.log(101003,"提交订单"+order.id)
		order.persistOrMerge()
	}
	
	//取货
	def finishOrder(long id){
		def now = new Date();
		Order order = Order.load(id)
		assert order : X("未找到订单"+ id)
		Diamond diamond = Diamond.loadByOrder(order)
		assert diamond : X("未找到对应钻石信息")

		assert diamond.factoryBill.factoryBillStatus == 3 : X("加工单未完成不能取货")
		assert diamond.org == loginService.currentOrg() : X("钻石库存位置不在本机构！请先走库存流程")
//		assert order.orderStatus == 4  : X("订单定制未完成不能取货")
		order.orderStatus = 5 //售出
		logService.log(101004,"订单取货"+order.id)
		//TODO save log
		order.persistOrMerge()
	}

	//加工单状态变更 
	def commitFactoryOrder(FactoryBill factoryBill){
		def now = new Date();
		Diamond diamond = Diamond.loadByFactoryBill(factoryBill)
		assert diamond : X("未找到对应钻石信息")
		
		def order = diamond.order
		assert order.orderStatus != 5  : X("用户已取货，不能修改加工单状态")
		
		logService.log(101010,"修改加工单（"+ factoryBill.id +"）状态为" + dictService.getItem("factoryBillStatus",factoryBill.factoryBillStatus).text)
		//TODO save log
		factoryBill.persistOrMerge()
	}
}